CS 211 Lesson 9
An Overview of Basic Programming
Quote:
Character isn't inherited. One builds it daily by how one thinks and acts Helen Gahagan Douglas
Lesson Objectives:
To review the first eight lessons
To review the important aspects of program creation, basic mathematical operations, and program control structures
To emphasize the concept of "readable" code
Lesson:
I. MATLAB Concepts
This is a review lesson -- no new MATLAB concepts are
introduced.
If you have not learned the material in previous lessons, please take the time required to review the material in those lessons.
II. Good Programming Practices
A. Review of programs
A MATLAB program consists of one (or more) functions in an m-file.
The m-file name should have the same name as the function it contains.
A good program requires:
"Readable" code that can be corrected and enhanced in the future by either the original program author or by different programmers.
Well documented code that facilitates future corrections and enhancements.
Efficient code that will execute in a reasonable amount of time.
The major cost of real-world software is not its original development costs, but rather the cost of future corrections and enhancements.
B. Creating "readable" code
"Readable" code is a series of MATLAB statements that facilitate quick understanding of the code's intent.
Consider the two coding examples below, both of which perform the exact same task. Which is more "readable"?
B = 1;
C = input('Enter a number: ');
D = C;
while abs(B - D) > 0.00001
D = B;
B = (D + C/D)/2;
end
fprintf('The answer is %f.\n', B)Error_tolerance = 0.00001;
New_guess = 1;
N = input('Enter a positive number: ');
Old_guess = N;
while abs(New_guess - Old_guess) > Error_tolerance
Old_guess = New_guess;
New_guess = (Old_guess + N/Old_guess)/2;
end
fprintf('The square root of %f is approximately %f.\n', ...
N, New_guess)
"Readable" code typically has the following characteristics:
Good, descriptive variable names.
Code statements that are grouped into related blocks by separating them from other code blocks with a blank line.
Code statements that are indented inside branches and loops to indicate selection or repetition.
Very descriptive prompts in input statements.
Very descriptive and well formatted output statements.
The creation of "constants" for important values (e.g., Error_tolerance in the above code).
C. An example program
Please use this sample solution from a previous semester's PEX 1 assignment as an example of good, well documented, readable code.
III. Algorithms
A. How to access "relative" values in an array
It is very common to calculate the position (index) of an array element.
For example, consider the problem of calculating a Fibonacci series, which is defined as follows:
f0 = 1
f1 = 1
fn = fn-1 + fn-2, for all n >= 3
The first 8 terms are: 1 1 2 3 5 8 13 21 ...One algorithm for calculating such a series uses an array to store previous values of the series and looks like the following:
n = input('Enter the Fibonacci term to calculate: ');
Values = ones(1,n);
for j = 3:n
Values(j) = Values(j-1) + Values(j-2);
end
fprintf('The %d term is %d\n', n, Values(n) );Notice the use of calculations to get the appropriate index values into the array.
Lab Work: Lab 9
References: (none)